<%@ page pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<style type="text/css">
	.ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
		div#rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}
	div#rMenu ul li{
		margin: 1px 0;
		padding: 0 5px;
		cursor: pointer;
		list-style: none outside none;
		background-color: #DFDFDF;
	}
	
	ul.ztree {
		margin-top: 10px;
		border: 1px solid #617775;
		background: #f0f6e4;
		width:500px;
		height:650px;
		padding-top: 25px;
		overflow-y:scroll;
		overflow-x:auto;
	}
</style> 
<div class="r_location">
	<i class="r_txtico"></i> 运营管理 &gt; 权限树管理
</div>

<div>
	<ul id="permisTreeDiv" class="ztree"></ul>
</div>

<div id="rMenu">
	<ul>
			<li id="p_add" onclick="addPermis();">增加权限</li>
			<li id="p_modify" onclick="modifyPermis();">修改权限</li>
			<li id="p_del" onclick="deletePermis();">删除权限</li>
	</ul>
</div>

<script>
	 
	
	
	// 权限树相关脚本
    var zNodes = new Array();
	<c:forEach var="permisTreeNode" items="${permisTreeNodeList}" varStatus="status">
		zNodes.push({ id:<c:out value="${permisTreeNode.id }" />, pId:<c:out value="${permisTreeNode.parentId }"/>, name:'<c:out value="${permisTreeNode.name }" />',
			open:<c:out value="${permisTreeNode.open }" />, 
			drag:<c:out value="${canDrag}" />,
			/* drag:<c:out value="${permisTreeNode.drag }" />,  */
			drop:<c:out value="${permisTreeNode.drop }" />,
			childOuter:<c:out value="${permisTreeNode.childOuter }" />, childOrder:<c:out value="${permisTreeNode.childOrder }" />,
			dropInner:<c:out value="${permisTreeNode.dropInner }" />, dropRoot:<c:out value="${permisTreeNode.dropRoot }" />,
			noR:<c:out value="${permisTreeNode.noR }"/>
		});
	</c:forEach> 

	var setting = {
		view : {
			expandSpeed : "",
			selectedMulti : false,
			dblClickExpand: false
		},
		edit : {
			enable : true,
			showRemoveBtn: false,
			showRenameBtn: false
		},
		check: {
			enable: false
		},
		data : {
			simpleData : {
				enable: true
			}
		},
		callback : {
			beforeDrag: beforeDrag,
			beforeDrop: beforeDrop,
			onRightClick: OnRightClick
		}
	};

	function filter(treeId, parentNode, childNodes) {
		if (!childNodes)
			return null;
		for ( var i = 0, l = childNodes.length; i < l; i++) {
			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
		}
		return childNodes;
	}
	
	function beforeDrag(treeId, treeNodes) {
		for (var i=0,l=treeNodes.length; i<l; i++) {
			if (treeNodes[i].drag === false) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 拖拽实现
 	 */
	function beforeDrop(treeId, treeNodes, targetNode, moveType) {
		var result = targetNode ? targetNode.drop !== false : true;
  		if(result == true) {
  		//	alert(moveType);
			var msg = null;
			if(moveType == 'inner') {
	 			if(targetNode != null) {
	 				msg = "确定要将权限【" + treeNodes[0].name + "】移动到【" + targetNode.name + "】权限下？";
	 			} else {
	 				msg = "确定要将权限【" + treeNodes[0].name + "】变为顶级权限？";
	 			}
	 			
				result = confirm(msg);
				
				if(result == true) {
					jQuery.ajax({
						url: 'permistree_manager/modify/drag?id=' + treeNodes[0].id + (targetNode == null ? '' : ('&targetId=' + targetNode.id)) + '&rand='+Math.random(),
						tokenId:'permistree_drag',	
						cache:false,
						async:false,
						success: function (data) {
							art.dialog({icon: data.success,content: data.message,lock:true});
							if(data.success == 'succeed') {
								result = true;
							} else {
								result = false;
							}
						}
					});
				} 
			} else if(moveType == 'next' || moveType == 'prev') {
				if(targetNode) {
					if(targetNode.getParentNode() && targetNode.getParentNode() != treeNodes[0].getParentNode()) {
						msg = "确定要将权限【" + treeNodes[0].name + "】移动到【" + targetNode.getParentNode().name + "】权限下？";
						result = confirm(msg);
						
						if(result == true) {
							jQuery.ajax({
								url: 'permistree_manager/modify/drag?id=' + treeNodes[0].id + '&targetId=' + targetNode.getParentNode().id + '&rand='+Math.random(),
								tokenId:'permistree_drag',	
								cache:false,
								async:false,
								success: function (data) {
									art.dialog({icon: data.success,content: data.message,lock:true});
									if(data.success == 'succeed') {
										result = true;
									} else {
										result = false;
									}
								}
							});
						} 
					} else if(!targetNode.getParentNode() && targetNode.getParentNode() != treeNodes[0].getParentNode()) {
						msg = "确定要将权限【" + treeNodes[0].name + "】变为顶级权限？";
						result = confirm(msg);
						
						if(result == true) {
							jQuery.ajax({
								url: 'cms/permistree_manager/modify/drag?id=' + treeNodes[0].id + '&rand='+Math.random(),
								tokenId:'permistree_drag',		
								cache:false,
								async:false,
								success: function (data) {
									art.dialog({icon: data.success,content: data.message,lock:true});
									if(data.success == 'succeed') {
										result = true;
									} else {
										result = false;
									}
								}
							});
						} 
					}
					
					
				}
			}
		} 

		return result;
	}
	
	/**
	 * 右键菜单的操作
	 */
	function OnRightClick(event, treeId, treeNode) {
		if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
			zTree.cancelSelectedNode();
			showRMenu("root", event.clientX, event.clientY + document.documentElement.scrollTop);
		} else if (treeNode && !treeNode.noR) {
			zTree.selectNode(treeNode);
			showRMenu("node", event.clientX, event.clientY + document.documentElement.scrollTop);
		}
	}
	
	function showRMenu(type, x, y) {
		$("#rMenu ul").show();
		if (type=="root") {
			$("#p_add").hide();
			$("#p_modify").hide();
			$("#p_del").hide();
		} else {
			$("#p_add").show();
			$("#p_modify").show();
			$("#p_del").show();
		}
		rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

		$("body").bind("mousedown", onBodyMouseDown);
	}
	
	function hideRMenu() {
		if (rMenu) rMenu.css({"visibility": "hidden"});
		$("body").unbind("mousedown", onBodyMouseDown);
	}
	
	function onBodyMouseDown(event){
		if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
			rMenu.css({"visibility" : "hidden"});
		}
	}
	
	/**
	 * 新增权限
	 */
	function addPermis() {
		hideRMenu();
		
		var treeNode = zTree.getSelectedNodes()[0];
		if(treeNode != null) {
			 showDialog('cms/permistree_manager/add?rand=' + Math.random(), {id:treeNode.id, name:encodeURI(treeNode.name)}, {width:'600px', title:'新增权限', lock:true});
		} else {
		 	showDialog('cms/permistree_manager/add?rand=' + Math.random(), {id:0, name:encodeURI('无')}, {width:'600px', title:'新增权限', lock:true});
		}
	}
	
	/**
	 * 删除权限
	 */
	function deletePermis() {
		hideRMenu();
		var nodes = zTree.getSelectedNodes();
		if (nodes && nodes.length>0) {
			if (nodes[0].children && nodes[0].children.length > 0) {
				var msg = "要删除的权限【" + nodes[0].name + "】是父权限，无法删除！";
				confirm(msg);
				
			} else {
				var msg = "确认要删除的权限【" + nodes[0].name + "】吗？";
				if (confirm(msg) == true){
					//zTree.removeNode(nodes[0]);
					jQuery.ajax({
						url: 'cms/permistree_manager/delete?id='+nodes[0].id + '&rand='+Math.random(),
						tokenId:'permistree_delete',
						success: function (data) {
							art.dialog({icon: data.success,content: data.message,lock:true});
							if(data.success == 'succeed') {
								zTree.removeNode(nodes[0]);
							}
						}
					});
				}
			}
		}
	}
	
	/**
	 * 修改权限
	 */
	function modifyPermis() {
		hideRMenu();
		
		var treeNode = zTree.getSelectedNodes()[0];
		
		showDialog('cms/permistree_manager/modify?rand=' + Math.random(), {id:treeNode.id,parentPermisName:
			(treeNode.getParentNode() == null || treeNode.getParentNode().name == null) ? encodeURI("无") : encodeURI(treeNode.getParentNode().name)}, 
				{width:'700px', title:'修改权限', lock:true});
	}

	var zTree, rMenu;
	$(document).ready(function() {
		$.fn.zTree.init($("#permisTreeDiv"), setting, zNodes);
		zTree = $.fn.zTree.getZTreeObj("permisTreeDiv");
		rMenu = $("#rMenu");
	});
	
	function showDialog(url, param, options){
		permMyDialog = art.dialog(options);// 初始化一个带有loading图标的空对话框
		jQuery.ajax({
			url: url,
			data : param,
			success: function (data) {
				permMyDialog.content(data);// 填充对话框内容
			}
		});
	}
	


</script>